package com.shawn.integeration.schedule.config;

import com.shawn.integeration.schedule.executor.MyScheduledThreadPoolExecutor;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;

/**
 * @author shawn
 * @description: 定时调度配置
 * @date 2024/1/16
 */
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
    // 设置任务调度器
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        // 自定义线程池作为调度器
        taskRegistrar.setScheduler(taskExecutor());
    }

    // 创建定时调度线程池
    @Bean(destroyMethod="shutdown")
    public ExecutorService taskExecutor() {
        ThreadFactory factory = new BasicThreadFactory.Builder()
                .namingPattern("schedule-pool-%d")
                .daemon(true)
                .build();

        return new MyScheduledThreadPoolExecutor(10, factory);
    }
}
